<?php
		
	/* SVN FILE: $Id: email-alert.php 65 2011-01-24 17:02:12Z andreoletti $ */
	
	/**
	 * Project Name : arcadia
	 *
	 * @author $Author: andreoletti $
	 * @version $Revision: 65 $
	 * @lastrevision $Date: 2011-01-24 18:02:12 +0100 (lun, 24 gen 2011) $
	 * @filesource $URL: http://svn.fletchlynd.com/arcadia/trunk/visioni/email-alert.php $
	 */


	require_once './include/functions.php';
	require_once './include/const.inc.php';
	// require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';

	include('Mail.php');
	include('Mail/mime.php');
	
	if (!isset($_SESSION)) { session_start(); }
	
	$sScript = fnCurrentScript();
	
	fnUpdateLog("EMAIL-ALERT SCRIPT RUN", 0, '', 'I');
	
	$sTable = fnTableDefault($sTABLE_DEFAULT, $aTABLE);
	
	fnTableConst($sTable);
	fnViewConst($sViewItemLink);
		
	// Array $aFieldsDataType
	fnFieldsDataTypeArray($sViewSelectFrom);

	$sFrom = "Ingegneria Clinica <bioingegneria@ospedaliriuniti.bergamo.it>";
	$crlf = "\n";
	
	$aTemplate = fnMailTemplate('VISIONI-WARNING-US');
	
	// INVIO EMAIL AL REPARTO PER SCADENZA A 7 GIORNI DAL TERMINE (PER LE PROVE VISIONI DI DURATA SUPERIORE A 14 GIORNI)
	
	$sQuery = " SELECT `v`.*,
		IF(`dtInizio`!='0000-00-00',DATEDIFF(`dtScadenza`,`dtInizio`),15) as `iDurata`,
		DATEDIFF(CURRENT_DATE(),`dtScadenza`) as `iScadenza`
		FROM `$sViewSelectFrom` `v`
		WHERE `tsMailScadenza` = '0000-00-00 00:00:00' AND `sUnitaEmail` != '' AND (`iRinnovo`='0' AND `iViewRinnovo`='0')
		AND `iVerifica`='0' AND `dtOut`='0000-00-00' AND `iArchiviato`='0' AND `iTrash`='0'
		HAVING `iDurata` > 14 AND `iScadenza` > -8";

	$rResult = custom_mysql_query($sQuery);

	if (mysql_num_rows($rResult) > 0) {
	
		while ($aData = mysql_fetch_assoc($rResult)) {	
		
			if (isset($aData['sUnitaEmail']) && is_valid_email($aData['sUnitaEmail'])) {
			
				$sHtml = utf8_decode($aTemplate['sHtml']);
				$sHtml = impress(ereg_replace("[\r\n]|$","</p>", $sHtml));
				$sSubject = utf8_decode($aTemplate['sSubject']);
				$sSubject = impress($sSubject);
				
				$sTo = $aData['sUnitaEmail']." <".$aData['sUnitaEmail'].">";
				$sCc = '';
				
				$aHdrs = array(
					'From'		=> $sFrom,
					'To'		=> $sTo,		
					'Cc'		=> $sCc,
					'Subject'	=> $sSubject
				); 
				
				$oMime = new Mail_mime($crlf);
				$oMime->setHTMLBody($sHtml);
				// do not ever try to call these lines in reverse order
				$sBody = $oMime->get();
				$aHdrs = $oMime->headers($aHdrs);
				$oMail =& Mail::factory('mail');
				$oMail->send($sTo, $aHdrs, $sBody);
				
				// Update tabella database
				$sQuery = "UPDATE `$sViewSelectFrom` SET `tsMailScadenza` = CURRENT_TIMESTAMP() WHERE `id`='".$aData['id']."'";
				$rTemp = custom_mysql_query($sQuery); // per questa query non posso chiamare il risultato $rResult altrimenti
													  // interferisce con il ciclo "while ($aData = mysql_fetch_assoc($rResult)) {"
				fnUpdateLog("EMAIL SENT: [".$sTo."]", $aData['id'], $sViewSelectFrom, 'I');
			
			} else {
				fnUpdateLog("EMAIL ADDRESS NOT VALID", $iView, $sViewSelectFrom, 'E');
			
			}

		}
		
	} else {
		fnUpdateLog("NO EMAIL SENT", 0, '', 'I');
	
	}

?>
